import JsbUtil from './JsbUtil';
/**
 * 用途         :Http请求模块
 * 开发者       :郑品延
 * 修改时间     :2019/8/6
 */
export default class HttpUtil{
    public static httpUrl () {
        return "https://app.meappstore.top/dressupgirlfree";
        return 'http://192.168.0.47:6808/dressupgirlfree';
        
        
        return "http://192.168.0.44:9432/optimum_calculation";
        return 'http://192.168.0.45:5555/yswgame';
        return 'https://loveaccout.zhappstore.vip/loveaccout';
    }

    public static nafosPost (url, param, callback?, failCallback?) { // 地址 , 结果返回
        if (!url || url == '') {
            return;
        }
        
        var newUrl = this.httpUrl() + url;
        JsbUtil.showLoading(true);
        Log("网络请求数据 查询数据 ---> ", newUrl, " --> ",JSON.stringify(param));
        if(cc.sys.platform == cc.sys.ANDROID){
        }
        this.sendPostResponese(newUrl, param, function (success, response) {
            JsbUtil.showLoading(false);
            if (success) {
                callback && callback(response)
            }else{
                failCallback && failCallback();
            }
        });
        
    }
    public static sendPostResponese (url, params, stateChangeCallback) {
            var xhr = new XMLHttpRequest();
            xhr.responseType = 'json';
            var time = false;//是否超时
            var timer = setTimeout(function () {
                if (timer == null) {
                    return;
                }
                time = true;
                //做请求超时操作
                xhr.abort();//请求中止
                Log("------------ 网络连接超时" );
                stateChangeCallback(false);
            }, 10000);

            xhr.onreadystatechange = function () {
                if (time)//请求已经超时，忽略中止请求
                {
                    return;
                }
                // Log("---- 网络连接状态 xhr = ", xhr.readyState, " -- ",xhr.status);
                if (xhr.readyState == 4) {
                    if(cc.sys.platform == cc.sys.ANDROID){
                        Log("网络请求数据 ---> ", JSON.stringify(xhr.response));
                    }
                    clearTimeout(timer);//取消等待的超时
                    timer = null;
                    if (xhr.status != 200) {
                        Log('请求服务器失败');
                        stateChangeCallback(false);
                    }else{
                        stateChangeCallback(true, xhr.response);
                    }
                }
            }
            let urladd = url + this.decodeParam(params);
            Log("--------------------------------- url ", urladd)
            xhr.open('POST', urladd, true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Accept", "application/json");
            xhr.send()
            // if (params) {
            //     xhr.send(JSON.stringify(params)); 
            //     Log('成功发送数据包:',JSON.stringify(params));               
            // }
            // else {
            //     xhr.send();
            //     Log('成功发送数据');               
            // }
            // return xhr;
    }

    //根据返回的错误状态码得到对应错误描述
    public static getTextWithStatus (status) {
        var msg = '';
        if (status == 800) {
            msg = '系统维护中';
        }
        else if (status == 801||403) {
            msg = '登录验证失败,请重新登陆';
        }
        if (status == 500) {
            msg = '服务器繁忙';
        }
        return msg;
    }

    public static decodeParam(param)
    {
        var str = '?';
        for(var i in param)
        {
            var key = i;
            var value = param[i];
            // if(value == null)
            // {
            //     value = '';
            // }
            // Log('key = ',key,'value = ',value)
            str = str + key + '=' + value + '&';
        }
        return str.substring(0,str.length-1) ;
    }
}
